import { Toast } from 'vant';
import cTitle from "components/title";
import { scrollMixin } from "../../utils/mixin";

export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      show:false,
      search_value:'',
      sort:{
        select:"",
        tab:""
      },
      order_field:'',
      order_by:'',
      minDate: new Date(),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      minDate_2: new Date(),
      maxDate_2: new Date(2025, 10, 1),
      currentDate_2: new Date(),
      typeBol:true,
      typeList:[{
        id:1,
        name:"快捷商务",
      },{
        id:2,
        name:"连锁酒店",
      },{
        id:3,
        name:"精品民宿",
      },{
        id:4,
        name:"精品公寓",
      },{
        id:5,
        name:"精品酒店",
      },{
        id:6,
        name:"星级酒店",
      }],
      selIndex:99999999999,
      selid:'',
      //定位
      location: {},
      hotelList:[],
      page: 1, //分页数，当前页数
      isLoadMore: true, //判断是否要加载更多的标志
      total_page: 0 //总页数
    };
  },
  computed: {

  },
  created() {

  },
  activated() {
    this.initData();
    this.initpageData();
    // this.location = this.$store.state.o2oLocation;
    // this.location = {
    //   city: "上海市",
    //   province: "上海市",
    //   point: {
    //     lat:'31.301071',
    //     lng:"121.487673"
    //   }
    // }
    // if (this.fun.isTextEmpty(this.location.point)) {
    // } else {
    //   this.gotoSearch();
    // }

    this.fun.getLocation()
      .then((res)=> {
        this.location = res;
        this.gotoSearch();
      })
      .catch((err)=> {
        console.log(err);
      });
  },
  mounted() {

  },
  methods: {
    typeSelectBtn(){
      this.typeBol = !this.typeBol;
    },
    selBtn(id,index){
      if(this.selIndex===index){
        this.selIndex=99999999;
        this.selid = '';
      }else{
        this.selIndex = index;
        this.selid = id;
      }
      this.typeBol=  true;
      this.initpageData();
      this.gotoSearch();
    },
    selectBtn(n) {
      this.typeBol = true;
      this.sort.tab = n;
      let order_field = '';
      if (n == 1) order_field = 'comment_num';
      if (n == 2) order_field = 'show_sales';
      if (n == 3) order_field = 'distance';
      if (this.sort.select == (n+'_2')) {
        this.sort.select = (n+'_1');
        this.order_field = order_field;
        this.order_by = 'desc';
      } else {
        this.sort.select = (n+'_2');
        this.order_field = order_field;
        this.order_by = 'asc';
      }
      this.initpageData();
      this.gotoSearch();
    },
    gotoSearchBtn(){
      this.initpageData();
      this.gotoSearch();
    },
    gotoSearch() {
      let p = this.fun.bd_encrypt(this.location.point.lng, this.location.point.lat);
      let json = {
        lng: p.lng,
        lat: p.lat,
        search:{
          city: this.location.city,
          province:this.location.province,
          name:this.search_value,
          level:this.selid,
          sort:this.order_by
        },
        page: this.page
      };
      $http
        .post("plugin.hotel-supply.frontend.home.index", json)
        .then(res => {
          if (res.result == 1) {
            this.isLoadMore = true;
            this.total_page = res.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.hotelList = res.data.data;
            console.log(this.hotelList);
          }else{
            this.hotelList = [];
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //获取更多数据
    getMoreData() {
      const that = this;
      that.isLoadMore = false;  // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let p = this.fun.bd_encrypt(this.location.point.lng, this.location.point.lat);
        let json = {
          lng: p.lng,
          lat: p.lat,
          search:{
            city: this.location.city,
            province:this.location.province,
            name:this.search_value,
            level:this.selid,
            sort:this.order_by
          },
          page: this.page
        };
        $http
          .post("plugin.hotel-supply.frontend.home.index", json)
          .then(res => {
            if (res.result == 1) {
              that.isLoadMore = true;
              if (res.result === 1) {
                var myData = res.data.data;
                that.hotelList = that.hotelList.concat(myData);//数组拼接
              } else {
                that.page = that.page - 1;
                that.isLoadMore = false;
                return;
              }
            }
          })
          .catch(error => {
            console.log(error);
          });
      }
    },
    initpageData() {
      this.hotelList = [];
      //more
      this.isLoadMore = true;
      this.page = 1;
      this.total_page = 0;
    },
    initData(){
      this.show=false;
      this.search_value='';
      this.sort = {
        select:"",
        tab:""
      };
      this.order_field = '';
      this.order_by='';

      this.minDate= new Date();
      this.maxDate =  new Date(2025, 10, 1);
      this.currentDate =  new Date();
      this.minDate_2 = new Date();
      this.maxDate_2 = new Date(2025, 10, 1);
      this.currentDate_2= new Date();
      this.typeBol=true;
      this.typeList=[{
        id:1,
        name:"快捷商务",
      },{
        id:2,
        name:"连锁酒店",
      },{
        id:3,
        name:"精品民宿",
      },{
        id:4,
        name:"精品公寓",
      },{
        id:5,
        name:"精品酒店",
      },{
        id:6,
        name:"星级酒店",
      }];
      this.selIndex=99999999999;
      this.selid='';
      //定位
      this.location= {};
      this.hotelList=[];
      this.page= 1; //分页数，当前页数
      this.isLoadMore=true; //判断是否要加载更多的标志
      this.total_page= 0; //总页数
    },
    hotelBtn(id){
      this.$router.push(this.fun.getUrl('HotelSupplyHome',{id:id}));
    }
  },
  components: { cTitle }
};


